<?php
use Core\SDException;
/**
 * 循环创建目录
 * @param string $dir
 * @param number $mode
 * @return boolean
 */
function makeDir ($dir='', $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode))
        return true;
    if (! makeDir(dirname($dir), $mode))
        return false;
    return @mkdir($dir, $mode);
}
/**
 * 检测空目录;
 * @param string $directory
 * @return boolean
 */
function emptyDir ($directory)
{
    // 打开目录
    if (is_dir($directory)) {
        $handle = opendir($directory);
        // 循环判断目录结构
        while (($file = readdir($handle)) !== false){
                if ($file != "." && $file != "..") {
                    //echo "".iconv('gbk', 'utf-8',  $directory."/".$file)."<br />";
                    if (is_dir($directory .DS. $file)) {                        
                        emptyDir($directory .DS. $file );
                    }
                   closedir($handle);
                   return false;
                }
            }
            closedir($handle);
        }
    return true;
}
/**
 * 清空缓存
 * @param string $path
 */
function clearCache ($path = APP_CACHE)
{
    if (is_dir($path)) {
        $files = scandir($path);
        foreach ($files as $file) {
            if ($file != "." && $file != "..") {
                clearCache($path .DS. $file);
            }
        }
    } else {
        if (@unlink($path)) {
            $path = iconv("gb2312", "utf-8", $path);
            echo "删除:" . $path . "成功!<br />";
        }
    }
}
// 获取客户端IP地址
function getClientIp ()
{
    static $ip = NULL;
    if ($ip !== NULL)
        return $ip;
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos = array_search('unknown', $arr);
        if (false !== $pos)
            unset($arr[$pos]);
        $ip = trim($arr[0]);
    } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
    return $ip;
}
/**
 * 发送头文件
 * @param unknown $code
 */
function sendHttpStatus ($code)
{
    static $_status = array(
            // Success 2xx
            200 => 'OK',
            // Redirection 3xx
            301 => 'Moved Permanently',
            302 => 'Moved Temporarily ', // 1.1
                                         // Client Error 4xx
            400 => 'Bad Request',
            403 => 'Forbidden',
            404 => 'Not Found',
            // Server Error 5xx
            500 => 'Internal Server Error',
            503 => 'Service Unavailable'
    );
    if (isset($_status[$code])) {
        header('HTTP/1.1 ' . $code . ' ' . $_status[$code]);
        // 确保FastCGI模式下正常
        header('Status:' . $code . ' ' . $_status[$code]);
    }
}
//获取ini文件
function iniGet($name,$process=true,$type="config"){
    $file = HEX_PATH.DS."Common".DS.'Hex.ini';
    if(file_exists($file)){
       $conf = parse_ini_file($file,$process);
       if($conf == false){
          throw new SDException($php_errormsg);
       }
       if($process){
           return $conf[$type][$name];
       }else{
           return $conf[$name];
       }
    }else{
        halt($file.' 文件不存在');
    }
    
}
//保存ini文件
function write_ini_file($path, $assoc_array) {
   $content = ";hexphp config";
   foreach($assoc_array as $key => $item) {
     if(is_array($item)) {
       $content .= "\n[{$key}]\n";
       foreach ($item as $key2 => $item2) {
         if(is_numeric($item2) || is_bool($item2))
           $content .= "{$key2}\t=\t{$item2}\n";
         else
           $content .= "{$key2}\t=\t\"{$item2}\"\n";
       }       
     } else {
       if(is_numeric($item) || is_bool($item))
         $content .= "{$key}\t=\t{$item}\n";
       else
         $content .= "{$key}\t=\t\"{$item}\"\n";
     }
   }       
   if(!$handle = fopen($path, 'w')) {
     return false;
   }
   if(!fwrite($handle, $content)) {
     return false;
   }
   fclose($handle);
   return true;
  } 

//获取语言
function L($name){
    static $langArr = array();
    $langArr = include_once HEX_PATH.DS."Language".DS.iniGet("language").".php";
    return $langArr[$name];
}